/**      
*  @Title:  PayloadBuild.java  
*  @Package  com.zjdiepu.www.util  
*    
*  @author  zhangpp
*  @date  2017年7月12日  下午2:33:54  
*  @version  V1.0      
*/
package com.zjdiepu.www.util;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ResultFlagType;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;

/**            
 *  项目名称：diepu-parent-base      
 *  类名称：PayloadBuild      
 *  类描述：      
 *  创建人：zhangpp
 *  创建时间：2017年7月12日  下午2:33:54      
 *  修改人：zhangpp
 *  修改时间：2017年7月12日  下午2:33:54      
 *  修改备注：      
 *  @version  V1.0          
 */
public class PayloadBuild 
{

	//ANDROID
	public static String appKeyAndorid = "f07262605b85c3f1bff17cda";
	public static String masterSecretAndorid = "dce6f9050ee89149de8d6c84";
	
	//IOS
	public static String appKeyIos = "dce6f9050ee89149de8d6c84";
	public static String masterSecretIos = "f07262605b85c3f1bff17cda";
	
	
	/**
	 因为IOS和Android通知的显示形式不一样，所以我们这两者分开写，分别单独推送。
	 根据别名、内容、标题，构建一个推送IOS平台的推送对象
	 这里可以对别名进行统一处理，譬如jpush不支持别名中含点号，而我们的账号中都是含有点号的，这里就可以统一替换，而不用每次调用时替换
            同样在客户端登陆时需要把点号换成"_"进行设置别名。
	* @author zhangpp
	* @Title: buildIOSPayload 
	* 
	* @param @param registrationId 唯一标示
	* @param @param alias 别名
	* @param @param alert 内容
	* @param @return    设定文件 
	* @return PushPayload    返回类型 
	* @version V1.0
	* @throws
	 */
	public static PushPayload buildIOSPayload(String registrationId,String alias,String alert) 
	{
		if(null == alert || ("").equals(alert))
		{
			throw new BaseException(ResultFlagType.FAIL.toString(),"推送内容不能为空，请检查！");
		}
		
		if (null != registrationId && !("").equals(registrationId)) 
		{
				 return PushPayload.newBuilder()
	             .setPlatform(Platform.ios_winphone())
	             .setAudience(Audience.registrationId(registrationId.replace(".", "_")))
	             .setNotification(Notification.newBuilder()
	             .setAlert(alert)
	             .build())
	             .build();
		}
		if (null != alias && !("").equals(alias)) 
		{
			 return PushPayload.newBuilder()
	             .setPlatform(Platform.ios_winphone())
	             .setAudience(Audience.alias(alias.replace(".", "_")))
	             .setNotification(Notification.newBuilder()
	             .setAlert(alert)
	             .build())
	             .build();
		}
		
		throw new BaseException(ResultFlagType.FAIL.toString(),"推送别名或者推送唯一标识不能为空！");
	 }
	
	/**
	 * 根据别名、内容、标题，构建一个推送Android和windows phone平台的推送对象
	* @author zhangpp
	* @Title: buildAndroidWinphonePayload 
	* 
	* @param @param registrationId  注册id
	* @param @param alias  别名
	* @param @param alert  内容
	* @param @param title  标题
	* @param @return    设定文件 
	* @return PushPayload    返回类型 
	* @version V1.0
	* @throws
	 */
	public static PushPayload buildAndroidWinphonePayload(String registrationId,String alias,String alert,String title) 
	{
	      if (null == alert || ("").equals(alert))
	      {
	    	  throw new BaseException(ResultFlagType.FAIL.toString(),"推送内容不能为空！");
		  }
	      if (null == title || ("").equals(title))
	      {
	    	  throw new BaseException(ResultFlagType.FAIL.toString(),"推送标题不能为空");
		  }
	      
		  if (null != registrationId && !("").equals(registrationId)) 
		  {
	        	return PushPayload.newBuilder()
	             .setPlatform(Platform.android())
	             .setAudience(Audience.registrationId(registrationId.replace(".", "_")))
	             .setNotification(Notification.newBuilder()
	             .setAlert(alert)
	             .addPlatformNotification(AndroidNotification.newBuilder()
	             .setTitle(title).build())
	             .build())
	             .build();
	      }
		  if (null != alias && !("").equals(alias)) 
		  {
			   return PushPayload.newBuilder()
	             .setPlatform(Platform.android())
	             .setAudience(Audience.alias(alias.replace(".", "_")))
	             .setNotification(Notification.newBuilder()
	             .setAlert(alert)
	             .addPlatformNotification(AndroidNotification.newBuilder()
	             .setTitle(title).build())
	             .build())
	             .build();
		  }
		  throw new BaseException(ResultFlagType.FAIL.toString(),"推送别名或者推送唯一标识不能为空！");
	}
	
	/**
	* 构建一个通知所有平台，所有设备的推送对象
	* @author zhangpp
	* @Title: buildPushPayload 
	* 
	* @param @param alert  通知内容
	* @param @return    设定文件 
	* @return PushPayload    返回类型 
	* @version V1.0
	* @throws
	 */
	public static PushPayload buildPushPayload(String alert)
	{
		if(null == alert || ("").equals(alert))
		{
			throw new BaseException(ResultFlagType.FAIL.toString(),"推送内容不能为空，请检查！");
		}
		return PushPayload.alertAll(alert);
	}
	
	public static void main(String[] args)
	{
	
		try 
		{
//			JPushClient client = new JPushClient(PayloadBuild.masterSecretAndorid, PayloadBuild.appKeyAndorid);
//			PushPayload payload = buildPushPayload("11");
//			PushResult result = client.sendPush(payload);
//	        System.out.println(result.getResponseCode());
	            
			//PushPayload payload = buildAndroidWinphonePayload("","dd17f259e7461c6bcd4e5aa54f022d00","标题","内容");
           
			JPushClient client = new JPushClient(PayloadBuild.appKeyIos,PayloadBuild.masterSecretIos);
			PushPayload payload = buildIOSPayload("", "8c8007695fda3492876029864396b5df", "哈喽");
			PushResult result = client.sendPush(payload);
	        System.out.println(result.getResponseCode());
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
